package com.jdrx.ocp.utils;

import com.jdrx.logger.bizlogs.BizLoggerService;
import com.jdrx.ocp.beans.commons.SysConstant;
import com.jdrx.sw.sewage.service.common.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@Component
public class LoggerUtil {
    private final static Logger LOG = LoggerFactory.getLogger(LoggerUtil.class);

    private final static Executor LOGGER_WORKER = Executors.newSingleThreadExecutor();

    private final static SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static BizLoggerService loggerService;

    private static RedisService redisService;

    @Autowired
    private LoggerUtil(BizLoggerService loggerService, RedisService redisService) {
        LoggerUtil.loggerService=loggerService;
        LoggerUtil.redisService=redisService;
    }

    /***
     * 发送操作日志
     * @param uid 人员id
     * @param apiDescription 调用的接口描述
     * @param msg 发送消息的主体
     * @return
     * @throws
     */
    public static void sendOperatingRecord(Long uid,String apiDescription,String msg) {
        String userName = redisService.getMapField(SysConstant.USER_KEY + uid, "realName");

        LOG.info(userName + ":" + SDF.format(new Date()) + ":" + apiDescription + ":" + msg);

        LOGGER_WORKER.execute(() -> loggerService.sendBizMessage(userName, apiDescription + ":" + msg));
    }
}
